<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<title>个人信息后台</title>
	</head>
	<body>
		<center>
			<h3>个人信息操作</h3>
			<?php
				date_default_timezone_set("PRC");			//设置默认时区
				


				require("../../../public/config.php");			//导入数据库文件
				
				$link = mysqli_connect(HOST,USER,PASS,DBNAME);		//连接数据库
				
				if(!$link){				//判断连接释放成功		
					die("数据库连接失败！原因：".mysqli_connect_error());
				}
				
				mysqli_set_charset($link,"utf8");		//设置数据库编码

				

				//根据a传来的值进行相应的判断
				switch ($_GET['a']){
					case "userupdate":							//用户修改资料
					//获取传过来的字段值
						$name = $_POST['name'];
						$sex = $_POST['sex'];
						$address = $_POST['address'];
						$code = $_POST['code'];
						$phone = $_POST['phone'];
						$email = $_POST['email'];
						$id = $_POST['id'];
						//拼接sql语句
						$sql = "update users set name='{$name}',sex='{$sex}',address='{$address}',code='{$code}',phone='{$phone}',email='{$email}' where id=".$id;
						
						mysqli_query($link,$sql);		//发送sql语句并执行
						
						if(mysqli_affected_rows($link)){		//判断执行是否成功
							echo '<script>alert("修改成功！"); </script>';
							header("refresh:0;url=index.php");
							exit();
						}else{
							echo '<script>alert("修改失败！"); </script>';
							header("refresh:0;url=index.php");
							exit();
						}
						break;
					case 'passupdate':							//用户修改密码

						if($_POST['oldpass']==""){				//判断是否为空
							echo '<script>alert("请输入原密码！"); </script>';
							header("refresh:0;url=pass.php");
							exit();
						}
						if($_POST['newpass']==""){				//判断新密码是否为空
							echo '<script>alert("新密码不能为空！"); </script>';
							header("refresh:0;url=pass.php");
							exit();
						}

						$oldpass = $_POST['oldpass'];			//接收旧密码
						$newpass = $_POST['newpass'];			//接收新密码

						$id = $_POST['id'];						//获取要修改的用户id
						$sql = "select pass from users where id=".$id;			//查找密码

						$result = mysqli_query($link,$sql);			//发送并执行sql语句
						$row = mysqli_fetch_assoc($result);

						if ($_POST['oldpass']!=$row['pass']) {		//判断旧密码是否正确
							echo '<script>alert("原密码错误！请重新输入！"); </script>';
							header("refresh:0;url=pass.php");
							exit();
						}else{
							$sql = "update users set pass='{$newpass}' where id=".$id;	//更新密码
							mysqli_query($link,$sql);
							if(mysqli_affected_rows($link)){		//判断执行是否成功
								echo '<script>alert("修改成功！请重新登录"); </script>';
								header("refresh:0;url=index.php");
								exit();
							}else{
								echo '<script>alert("修改失败！"); </script>';
								header("refresh:2;url=pass.php");
								exit();
							}
						}
						break;
				}
				mysqli_close($link);		//关闭数据库
				
			?>
		</center>
	</body>
</html>